package com.semlab.client.mvp.home.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Widget;
import com.semlab.client.proxy.MovieProxy;
import com.semlab.client.proxy.ResourceProxy;
import com.semlab.client.widgets.Hyperlink;
import com.semlab.client.widgets.InlinePanel;
import com.semlab.client.widgets.PropertyTable;
import com.semlab.client.widgets.PropertyTableInline;
import com.semlab.shared.DBpediaResources;

public class MovieDetails extends Composite {

    private static MovieDetailsUiBinder uiBinder = GWT.create(MovieDetailsUiBinder.class);

    interface MovieDetailsUiBinder extends UiBinder<Widget, MovieDetails> {
    }

    private MovieProxy movie;

    @UiField
    Image imageContainer;

    @UiField
    PropertyTable propertyTable;

    public MovieDetails() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    public void setMovie(MovieProxy movie) {
        GWT.log("setting movie: " + movie);
        propertyTable.clear();
        if (movie.getImageUrl() != null) {
            imageContainer.setUrl("data:image/jpg;base64," + movie.getImageUrl());
            imageContainer.setVisible(true);
        } else {
            imageContainer.setVisible(false);
        }
        propertyTable.insertWidget("Title", movie.getTitle());
        if (movie.getWikiAbstract() != null)
            propertyTable.insertWidget("Abstract", movie.getWikiAbstract());
        if (movie.getGenre() != null)
            propertyTable.insertWidget("Genre", movie.getGenre());
        if (movie.getPlot() != null)
            propertyTable.insertWidget("Plot", movie.getPlot());
        if (movie.getBudget() != null)
            propertyTable.insertWidget("Budget", movie.getBudget());
        if (movie.getRuntime() != null) {
            try {
                propertyTable.insertWidget("Running time", ((((Double) Double.parseDouble(movie.getRuntime())).intValue() / 60)) + " minutes");
            } catch (Exception e) {
                propertyTable.insertWidget("Running time", movie.getRuntime());
            }
        }
//        if (movie.getBu != null)
//            propertyTable.insertWidget("Genre", movie.getGenre());
        if (movie.getResources() != null) {
//                VerticalPanel vertical = new VerticalPanel();
//                for (ActorProxy proxy : movie.getActors()) {
//                    Hyperlink link = new Hyperlink(proxy.getName(), proxy.getWikiUri(), true);
//                    vertical.add(link);
//                }

//                GWT.log(movie.getResources()+"");
            for (DBpediaResources dBpediaResources : DBpediaResources.values()) {
                InlinePanel panel = new InlinePanel();
                for (ResourceProxy proxy : movie.getResources()) {
                    if (proxy.getLink().equals(dBpediaResources.getUri())) {
                        Hyperlink link = new Hyperlink(proxy.getName(), proxy.getWikiUri(), true);
                        if (panel.getWidgetCount() > 0) {
                            InlineHTML html = new InlineHTML("&#8226;");
                            html.getElement().getStyle().setProperty("margin", "0px 5px");
                            panel.add(html);
                        }
                        panel.add(link);
                    }
                }
                if (panel.getWidgetCount() > 0)
                    propertyTable.insertWidget(dBpediaResources.getLabel(), panel);
            }

//                propertyTable.insertWidget("Starring", vertical);
        }

//        propertyTable.insertWidget("Facebook", new Hyperlink(movie.getFbUri(), true));
//        if (movie.getWikiUri() != null && !movie.getWikiUri().equals(""))
//            propertyTable.insertWidget("Wikipedia", new Hyperlink(movie.getWikiUri(), true));
        if (movie.getUri() != null && !movie.getUri().equals("")) {
            propertyTable.insertWidget("DbPedia", new Hyperlink(movie.getUri(), true));
        }
//        if (movie.getImdbUri() != null)
//            propertyTable.insertWidget("IMDB", new Hyperlink(movie.getImdbUri(), true));

    }

/*        private class IconLabel extends Composite {
            private HorizontalPanel container;

            public IconLabel(ImageResource resource, String label) {
                container = new HorizontalPanel();
                container.add(new Image(resource));
                container.add(new Label(label));
                initWidget(container);
            }
        }*/


}
